/* ProxyVerifier.java
 * Author: liuex
 * 2007-12-19 ����02:01:03
 *
 * Note: 
 */
package lex.proxyverfier;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Formatter;
import java.util.List;

import javax.swing.ActionMap;
import javax.swing.JFrame;

import org.lex.app.Application;
import org.lex.net.proxy.ProxyServer;
import org.lex.net.proxy.ProxyServerUtils;
import org.lex.swing.Swings;

import lex.proxyverfier.actions.*;
import lex.proxyverfier.prefs.Verifications;
import lex.proxyverfier.verifier.ThreadPool;

public class ProxyVerifier extends Application
{
	private static final String proxyFile = "proxies.txt";
	private JFrame mainFrame = null;
	private MainContentPane contentPane = null;
	private Verifications verifications = null;

	public static ProxyVerifier getApplication()
	{
		return Application.getApplication( ProxyVerifier.class );
	}

	public MainContentPane getContentPane()
	{
		if( null == this.contentPane )
		{
			this.contentPane = new MainContentPane();
		}
		return this.contentPane;
	}

	public Formatter getFormatter()
	{
		return this.getContentPane().getFormatter();
	}

	public ProxyTableModel getProxyManager()
	{
		return this.getContentPane().getProxyTableModel();
	}

	public Verifications getVerifications()
	{
		if( null == this.verifications )
		{
			verifications = Verifications.decode( getContext().getResourceManager()
				.getResourceMap().getString( CK.VERIFICATIONS ) );
		}
		return this.verifications;
	}

	public JFrame getRootWindow()
	{
		if( null == mainFrame )
		{
			mainFrame = new JFrame();
			mainFrame.setName( getClass().getCanonicalName() + ".mainframe" );
			mainFrame.setTitle( "Proxy Verifier" );
			mainFrame.setContentPane( this.getContentPane() );
			mainFrame.setJMenuBar( new MainMenuBar() );
		}
		return this.mainFrame;
	}

	@Override
	public String getApplicationID()
	{
		return "ProxyVerifier";
	}

	protected void initialize( String[] args )
	{
		this.initializeGlobalActionMap();
		this.restoreSession();
		this.getRootWindow().setVisible( true );
	}

	private void initializeGlobalActionMap()
	{
		ActionMap am = this.getContext().getActionManager().getActionMap();
		am.put( "add", new AddAction() );
		am.put( "remove", new RemoveAction() );
		am.put( "about", new AboutAction() );
		am.put( "verify", new VerifyAction() );
		am.put( "exportPlain", new ExportPlainFileAction() );
		am.put( "importPlain", new ImportPlainFileAction() );
		am.put( "preferences", new PreferencesAction() );
	}

	private void restoreSession()
	{
		this.getContext().getSessionStorage().restore( this.getRootWindow() );
		// restore proxies
		File file = new File( this.getContext().getLocalStorage().getApplicationHome(), proxyFile );
		if( file.exists() && file.isFile() )
		{
			try
			{
				List<ProxyServer> servers = ProxyServerUtils.importPlain( new FileReader( file ) );
				ImportExportOperations.importProxyServers( servers );
			}
			catch( IOException e )
			{
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void shutdown()
	{
		this.getContext().getResourceManager().getResourceMap().putProperty( CK.VERIFICATIONS,
			this.getVerifications().toString() );
		this.getContext().getSessionStorage().save( this.getRootWindow() );
		// save proxies
		File file = new File( this.getContext().getLocalStorage().getApplicationHome(), proxyFile );
		try
		{
			ImportExportOperations.exportPlain( this.getProxyManager().getProxyServerList(), file );
		}
		catch( IOException e )
		{
			e.printStackTrace();
		}
		ThreadPool.shutdownNow();
	}

	public static void main( String[] args )
	{
		Swings.setUseBoldMetalFont( false );
		launch( ProxyVerifier.class, args );
	}
}
